public class Test {
    public static int getSum(String s,int i ,int sum){
        switch (s.charAt(i)) {
            case 'I':
                sum += 1;
                break;
            case 'V':
                sum += 5;
                break;
            case 'X':
                sum += 10;
                break;
            case 'L':
                sum += 50;
                break;
            case 'C':
                sum += 100;
                break;
            case 'D':
                sum += 500;
                break;
            case 'M':
                sum += 1000;
                break;
            default:
                break;
        }
        return sum;
    }
    public static int romanToInt(String s) {
        int sum = 0;
        for (int i = 0, j = 1; i < s.length(); ) {
         if(j>=s.length()){
            sum= getSum(s,i,sum);
             i++;
             continue;
         }
            String tmp = s.substring(i, j + 1);
            if (tmp.equals("IV")) {
                sum += 4;
                i += 2;
                j += 2;
            } else if (tmp.equals("IX")) {
                sum += 9;
                i += 2;
                j += 2;
            } else if (tmp.equals("XL")) {
                sum += 40;
                i += 2;
                j += 2;
            } else if (tmp.equals("XC")) {
                sum += 90;
                i += 2;
                j += 2;
            } else if (tmp.equals("CD")) {
                sum += 400;
                i += 2;
                j += 2;
            } else if (tmp.equals("CM")) {
                sum += 900;
                i += 2;
                j += 2;
            } else {
               sum =  getSum(s,i,sum);
                i++;
                j++;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
       int sum = romanToInt("III");
        System.out.println(sum);
    }
}
